package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.IDao;
import com.mohe.dangjian.model.CommunityGeneralParty;
import com.mohe.dangjian.service.interfaces.ICommunityGeneralParty;
import com.mohe.dangjian.utils.Page;

public class CommunityGeneralPartyImpl implements ICommunityGeneralParty {

	private IDao<CommunityGeneralParty> communityGeneralPartyDao;
	
	public IDao<CommunityGeneralParty> getCommunityGeneralPartyDao() {
		return communityGeneralPartyDao;
	}

	public void setCommunityGeneralPartyDao(
			IDao<CommunityGeneralParty> communityGeneralPartyDao) {
		this.communityGeneralPartyDao = communityGeneralPartyDao;
	}

	@Override
	public List<CommunityGeneralParty> getCommunityGeneralPartyList(
			CommunityGeneralParty communityGeneralParty, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityGeneralParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 党组织名称
		if (!StringUtils.isBlank(communityGeneralParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityGeneralParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(communityGeneralParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + communityGeneralParty.getAddress() + "%%");
		}
		// 居委会电话
		if (!StringUtils.isBlank(communityGeneralParty.getTel())) {
			where.append(" and tel like ?");
			params.add("%%" + communityGeneralParty.getTel() + "%%");
		}
		// 党支部书记
		if (!StringUtils.isBlank(communityGeneralParty.getSecretary())) {
			where.append(" and secretary like ?");
			params.add("%%" + communityGeneralParty.getSecretary() + "%%");
		}
		// 党支部书记联系方式
		if (!StringUtils.isBlank(communityGeneralParty.getTel1())) {
			where.append(" and tel1 like ?");
			params.add("%%" + communityGeneralParty.getTel1() + "%%");
		}
		// 居委会主任
		if (!StringUtils.isBlank(communityGeneralParty.getDirector())) {
			where.append(" and director like ?");
			params.add("%%" + communityGeneralParty.getDirector() + "%%");
		}
		// 居委会主任联系方式
		if (!StringUtils.isBlank(communityGeneralParty.getTel2())) {
			where.append(" and tel2 like ?");
			params.add("%%" + communityGeneralParty.getTel2() + "%%");
		}

		where.append(" order by createDate desc");
		List<CommunityGeneralParty> list = communityGeneralPartyDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(CommunityGeneralParty communityParty) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityGeneralParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// TODO 查询条件待补充
		// 党组织名称
		if (!StringUtils.isBlank(communityParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(communityParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + communityParty.getAddress() + "%%");
		}
		// 居委会电话
		if (!StringUtils.isBlank(communityParty.getTel())) {
			where.append(" and tel like ?");
			params.add("%%" + communityParty.getTel() + "%%");
		}
		// 党支部书记
		if (!StringUtils.isBlank(communityParty.getSecretary())) {
			where.append(" and secretary like ?");
			params.add("%%" + communityParty.getSecretary() + "%%");
		}
		// 党支部书记联系方式
		if (!StringUtils.isBlank(communityParty.getTel1())) {
			where.append(" and tel1 like ?");
			params.add("%%" + communityParty.getTel1() + "%%");
		}
		// 居委会主任
		if (!StringUtils.isBlank(communityParty.getDirector())) {
			where.append(" and director like ?");
			params.add("%%" + communityParty.getDirector() + "%%");
		}
		// 居委会主任联系方式
		if (!StringUtils.isBlank(communityParty.getTel2())) {
			where.append(" and tel2 like ?");
			params.add("%%" + communityParty.getTel2() + "%%");
		}

		return communityGeneralPartyDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveCommunityGeneralParty(CommunityGeneralParty communityParty) {
		return communityGeneralPartyDao.insert(communityParty);
	}

	@Override
	public boolean saveCommunityGeneralPartyList(
			List<CommunityGeneralParty> communityGeneralPartyList) {
		return communityGeneralPartyDao.insert(communityGeneralPartyList);
	}

	@Override
	public CommunityGeneralParty getCommunityGeneralPartyById(Long timeKey) {
		CommunityGeneralParty communityGeneralParty = communityGeneralPartyDao.getByKey(timeKey);
		if (communityGeneralParty != null) {
			return communityGeneralParty;
		}
		return null;
	}

	@Override
	public boolean updateCommunityGeneralParty(CommunityGeneralParty communityGeneralParty) {
		return communityGeneralPartyDao.update(communityGeneralParty);
	}

	@Override
	public boolean delCommunityGeneralParty(CommunityGeneralParty communityGeneralParty) {
		return communityGeneralPartyDao.delete(communityGeneralParty);
	}

	@Override
	public List<CommunityGeneralParty> getAllCommunityGeneralParty() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return communityGeneralPartyDao.query(where.toString(), null);
	}
}
